package service.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import service.edu.exception.CustomException;
import service.edu.result.R;
import service.edu.utils.AliyunVodSDKUtil;
import service.vod.utlis.ConstantProperties;


@CrossOrigin
@RequestMapping("/vod/video")
@RestController
@Api(description="阿里云视频点播微服务")
public class VideoController {

    @GetMapping("getPlayAuth/{videoId}")
    public R getPlayAuth(@PathVariable String videoId){
         String keyId =ConstantProperties.KEY_ID;
         String keySecret= ConstantProperties.KEY_SECRET;
        try {
            //初始化
            DefaultAcsClient client = AliyunVodSDKUtil.initVodClient(keyId, keySecret);
            //请求
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoId);
            //响应
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            //得到播放凭证
            String playAuth = response.getPlayAuth();
            //返回结果
            return R.Ok().message("获取凭证成功").data("playAuth", playAuth);
        } catch (ClientException e) {
            throw new CustomException(20001,"获取视频播放凭证失败");
        }
    }
}
